package com.cexy.springboot_shiro_redis.web;

import com.cexy.springboot_shiro_redis.dao.domain.SysUser;
import com.cexy.springboot_shiro_redis.service.UserService;
import com.cexy.springboot_shiro_redis.utils.RequestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Set;


@Controller
@RequestMapping(value = "/perm")
public class PermInfoController {

    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    @RequiresPermissions({"usersPage"})
    @ResponseBody
    public String list(){
        SysUser user = RequestUtils.currentLoginUser();
        if(user != null) {
            Set<String> authorization = userService.findPermissionsByUserId(user.getId());
            return "该用户有如下权限" + authorization;
        }
        return "无登录用户";
    }

    @RequestMapping(value = "/test")
    public ModelAndView test(ModelAndView mv) {
        SysUser user = RequestUtils.currentLoginUser();
        mv.setViewName("/auth");
        mv.addObject("user",user.getUserName());
        mv.addObject("title","欢迎使用Thymeleaf!");
        return mv;
    }
}
